Grokking Tree Coding Patterns for Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Course Overview
Introduction to Tree
Types of Tree
BST Traversal Techniques
BST Operations
Introduction to Level Order Traversal Pattern
Reverse Level Order Traversal (easy)
Solution: Reverse Level Order Traversal
Find Largest Value in Each Tree Row (medium)
Solution: Find Largest Value in Each Tree Row
Maximum Width of Binary Tree (medium)
Solution: Maximum Width of Binary Tree
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Zigzag Traversal (medium)
Solution: Zigzag Traversal
Even Odd Tree (medium)
Solution: Even Odd Tree
N-ary Tree Level Order Traversal (hard)
Solution: N-ary Tree Level Order Traversal
Introduction to Tree Depth Pattern
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Minimum Depth of a Binary Tree (easy)
Solution: Minimum Depth of a Binary Tree
Balanced Binary Tree (easy)
Solution: Balanced Binary Tree
Tree Diameter (easy)
Solution: Tree Diameter
Maximum Depth of N-ary Tree (easy)
Solution: Maximum Depth of N-ary Tree
Find maximum depth of subtree (medium)
Solution: Find maximum depth of subtree
Root to Leaf Path Pattern (medium)
Introduction to Root to Leaf Path Pattern
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Sum of Root To Leaf Binary Numbers (easy)
Solution: Sum of Root To Leaf Binary Numbers
Insufficient Nodes in Root to Leaf Paths (medium)
Solution: Insufficient Nodes in Root to Leaf Paths
Smallest String Starting From Leaf (medium)
Solution: Smallest String Starting From Leaf
Pseudo-Palindromic Paths in a Binary Tree (medium)
Solution: Pseudo-Palindromic Paths in a Binary Tree
All Paths for a Sum (medium)
Solution: All Paths for a Sum
Sum of Path Numbers (medium)
Solution: Sum of Path Numbers
Path With Given Sequence (medium)
Solution: Path With Given Sequence
Count Paths for a Sum (medium)
Solution: Count Paths for a Sum
Leaf Processing Pattern (medium)
Introduction to Leaf Processing Pattern
Sum of Left Leaves (easy)
Solution: Sum of Left Leaves
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Check if all leaves are at same level (medium)
Solution: Check if all leaves are at same level
Delete Leaves With a Given Value (medium)
Solution: Delete Leaves With a Given Value
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
Tree View Pattern (medium)
Introduction to Tree View Pattern
Right View of a Binary Tree (medium)
Solution: Right View of a Binary Tree
Top View Of the Binary Tree (medium)
Solution: Top View Of the Binary Tree
Bottom View Of the Binary Tree (medium)
Solution: Bottom View Of the Binary Tree
Boundary of Binary Tree (medium)
Solution: Boundary of Binary Tree
Comparison of Two Trees Pattern (medium)
Introduction to Comparison of Two Trees Pattern
Symmetric Tree (easy)
Solution: Symmetric Tree
Mirror Image Trees (medium)
Solution: Mirror Image Trees
Subtree of Another Tree (easy)
Solution: Subtree of Another Tree
Isomorphic Trees (medium)
Solution: Isomorphic Trees
Introduction to Serialize and Deserialize Tree Pattern
Serialize and Deserialize BST (medium)
Solution: Serialize and Deserialize BST
Verify Preorder Serialization of a Binary Tree (medium)
Solution: Verify Preorder Serialization of a Binary Tree
Create Binary Tree From Descriptions (medium)
Solution: Create Binary Tree From Descriptions
Serialize and Deserialize N-ary Tree (hard)
Solution: Serialize and Deserialize N-ary Tree
Solution: Symmetric Tree

Problem Statement

Given the root of a binary tree, return true if the tree is symmetric, meaning it is a mirror image of itself. Otherwise, return false.

A tree is considered symmetric if the left subtree is a mirror reflection of the right subtree.

Examples

Example 1:

  • Input: root = [1,5,5,3,4,4,3]
Image
  • Expected Output: true
  • Justification: The tree is symmetric because the left subtree mirrors the right subtree at every level.

Example 2:

  • Input: root = [1,5,5,null,3,null,3]
Image
  • Expected Output: false
  • Justification: The tree is not symmetric because the left and right subtrees do not match in structure or values.

Example 3:

  • Input: root = [1,2,2,null,3,3,null]
Image
  • Expected Output: true
  • Justification: The tree is symmetric because the left and right subtrees are mirror images in terms of structure and values.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100

Solution

To solve this problem, the goal is to check whether a binary tree is symmetric. A tree is symmetric if the left and right subtrees of the root are mirror images of each other. To achieve this, the recursive approach known as Depth First Search (DFS) is used to compare the left and right subtrees simultaneously.

The approach begins by checking whether the tree is symmetric starting from the root. A helper method, isMirror, is used to compare two nodes (starting with the root and itself). This method ensures that:

  • Both nodes are null (in which case, they are considered mirrors).
  • One node is null and the other is not (which means the tree is not symmetric).
  • Both nodes have the same value, and the left subtree of one node is compared to the right subtree of the other.

This approach ensures that every corresponding node in the left and right subtrees are compared in a mirror fashion. By checking these conditions at every step, the algorithm confirms if the tree is symmetric or not.

Step-by-Step Algorithm

  1. Call the helper method isMirror(TreeNode t1, TreeNode t2):

    • Pass the root node twice to the isMirror method to compare the left and right subtrees.
    • We begin by checking whether both t1 and t2 are null.
  2. Handle null nodes:

    • If both nodes are null, return true because two null nodes are mirrors of each other.
    • If one node is null and the other is not, return false because they are not mirrors.
  3. Compare the values of the nodes:

    • If neither node is null, compare their values (t1.val == t2.val).
    • If the values are not equal, return false because the tree is not symmetric.
    • If the values are equal, continue to the next step.
  4. Recursive comparison:

    • Check the left subtree of t1 with the right subtree of t2 by recursively calling isMirror(t1.left, t2.right).
    • Simultaneously, check the right subtree of t1 with the left subtree of t2 by recursively calling isMirror(t1.right, t2.left).
  5. Return the result:

    • Both recursive calls must return true for the current pair of nodes to be considered mirrors. If both are true, the current nodes are mirrors. Otherwise, return false.

Algorithm Walkthrough

Input: [1,5,5,3,4,4,3]

Image
  1. Initial Step:

    • The isSymmetric() method is called with the root node of the tree (value 1).
  2. First Call to isMirror():

    • The isMirror() method is called with both t1 and t2 pointing to the root node (value 1).
    • Since t1.val == t2.val (both have value 1), we move forward to check their subtrees.
  3. Check the Left and Right Subtrees:

    • Now, isMirror(t1.left, t2.right) is called to compare the left subtree of t1 and the right subtree of t2.
    • At the same time, isMirror(t1.right, t2.left) is called to compare the right subtree of t1 and the left subtree of t2.
    • Both pairs have nodes with value 5. Since 5 == 5, we move deeper into their subtrees.
  4. Second Level: Compare Subtrees:

    • Next, isMirror(t1.left.left, t2.right.right) is called to compare the left child of t1.left (value 3) with the right child of t2.right (value 3).
    • Also, isMirror(t1.left.right, t2.right.left) is called to compare the right child of t1.left (value 4) with the left child of t2.right (value 4).
    • Both comparisons return true because 3 == 3 and 4 == 4.
  5. Handle the Leaf Nodes:

    • At this point, the algorithm reaches the leaf nodes (null values).
    • The isMirror(null, null) calls check these and return true, since both sides are null.
  6. Return the Result:

    • As all recursive calls at each level have returned true, the algorithm concludes that the tree is symmetric.
    • The isSymmetric() method returns true, confirming that the tree [1,5,5,3,4,4,3] is indeed symmetric.

Code

Java

. . . .

Complexity Analysis

Time Complexity

The isMirror method recursively checks every node in the tree to determine whether the tree is symmetric. Each node is visited once, meaning that for n nodes, the algorithm runs in time.

Space Complexity

The space complexity is determined by the recursion stack. In the worst case (a skewed tree), the depth of the recursion could be n, leading to a space complexity of . However, for a balanced tree, the recursion depth would be .

Symmetric Tree (easy)
Mirror Image Trees (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity